/**
 * 2025/01/01 - 2025/12/31
 */
package com.snail.server.database.engine.datasource.enums;

import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.annotation.IEnum;
import com.baomidou.mybatisplus.extension.toolkit.JdbcUtils;
import lombok.Getter;
import lombok.ToString;

import java.util.Arrays;

/**
 *
 * @author zgadmin
 * @date 2025/8/28 16:32
 *
 */
@Getter
@ToString
public enum DataSourceTypeEnum implements IEnum<String> {

    MYSQL(DbType.MYSQL,"mysql数据库"),
    ORACLE(DbType.ORACLE, "oracle数据库"),
    POSTGRE_SQL(DbType.POSTGRE_SQL, "Postgre数据库"),
    SQLITE(DbType.SQLITE, "SQLite数据库"),
    OTHER(DbType.OTHER, "其他数据库"),
    ;

    private final String value;

    private final String desc;

    DataSourceTypeEnum(DbType dbType, String desc){
        this.value = dbType.getDb();
        this.desc = desc;
    }

    public static DataSourceTypeEnum getEnums(String jdbcUrl){
        if (null == jdbcUrl || "".equals(jdbcUrl)) {
            return OTHER;
        }
        DbType dbType = JdbcUtils.getDbType(jdbcUrl);
        return Arrays.stream(values()).filter(e -> e.getValue().equals(dbType.getDb())).findFirst().orElse(OTHER);
    }
}